package com.dkm.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Department;
import com.dkm.service.DepartmentService;
import com.dkm.util.LayuiJsonUilt;
import com.dkm.util.ResultUtil;

@RestController
@RequestMapping("/department")
public class DepartmentController {

	//注入部门业务层
	@Autowired
	private DepartmentService departmentService;
	
	//多条件分页查询部门
	@GetMapping
	@RequiresPermissions(value = "department:query")
	public LayuiJsonUilt<Department> name(Department department,LayuiJsonUilt<Department> lj) {
		//获取jpa分页对象
		Pageable pageable=PageRequest.of(lj.getPage(), lj.getLimit());
		
		Page<Department> page=departmentService.findAllDepartments(department, pageable);
		
		//设置数据
		lj.setData(page.getContent());
		//设置数据总条数
		lj.setCount(page.getTotalElements());
		
		return lj;
	}
	
	//添加部门
	@PostMapping
	@RequiresPermissions(value = "department:add")
	public ResultUtil addDepartment(Department department) {
		
		return departmentService.saveAndFlushDepartment(department);
	}
	
	//修改部门
	@PutMapping
	@RequiresPermissions(value = "department:update")
	public ResultUtil updateDepartment(Department department) {
		
		return departmentService.saveAndFlushDepartment(department);
	}
	
	//删除部门
	@DeleteMapping
	@RequiresPermissions(value = "department:delete")
	public ResultUtil deleteDepartment(String department_id) {
		
		return departmentService.deleteDepartment(department_id);
	}
	
	//获取全部部门信息
	@GetMapping("/getAllDepartments")
	public List<Department> getAllDepartments() {
		
		return departmentService.getAllDepartments();
	}
}
